<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/5/25
  Time: 20:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*" pageEncoding="UTF-8" %>
<%@ page import="com.whu.Table" %>
<%@ page import="com.whu.DBTest" %>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>webForDatabase</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <style type="text/css">@import "style.css";</style>
    <script src="jquery-2.2.3.min.js"></script>
    <script type="text/javascript">

        function change() {
            $("#hid").fadeToggle();
        }
        function successful(){
            alert("operate successfully");
        }
    </script>
</head>
<%
    String sql = "select * from student order by rowNumber";
    if (session.getAttribute("sql") != null) {
        sql = (String) session.getAttribute("sql");
    }

    String tableName = "";
    if (session.getAttribute("tableNam") != null) {
        tableName = (String) session.getAttribute("tableNam");
        Table.setTableName(tableName);
    }

    //执行select查询
    Table table = DBTest.query(sql);
    String[][] matrix = table.getMatrix();
    session.setAttribute("table",table);
    //第一次加载为空，后面加载就直接从table属性中读取
    String[] tableList;
    if(Table.getTableList()==null){
        tableList = DBTest.getTables();
    }else {
        tableList = Table.getTableList();
    }
%>
<body>
<h1 align="center">数据库展示</h1>

<div class="sqlDiv">
    <form action="myServlet" method="post">
        <textarea class="sqlBlock" name = "sqlFillIn"><%=sql%></textarea>
        <input class="submitBtn" type="submit" value="submit" name="op" />
    </form>
</div>

<div class="tableDiv" id="table">
    <table class="table" border="1">
        <tr>
            <% for (int i = 0; i < table.getColumn(); i++) {
            %>
            <td class="tableHeader">
                <%= table.getElements()[i]%>
            </td>
            <%
                }
            %>
        </tr>
        <%
            for (int i = 0; i < table.getRow(); i++) {
        %>
        <tr>
            <%
                for (int j = 0; j < table.getColumn(); j++) {
            %>
            <td>
                <%=matrix[i][j] %>
            </td>

            <%
                }
            %>
        </tr>
        <%
            }
        %>
    </table>
</div>

<div class="operationDiv">
    <button class="operateBtn" type="button" id="operateButton" onclick="change()">operate</button>
    <form action="myServlet" method="post">
        <input class="operateBtn" type="submit" value="select" name="op" />
        <br>
        <select class="operateBtn" name="tableName" style="background-color: #e9e9e9;">
            <option value="<%= tableName%>"><%= tableName%></option>
            <%
            for(int i = 0;i<tableList.length;i++){
            %>
            <option value="<%= tableList[i] %>"><%= tableList[i] %></option>
            <%
                }
            %>
            <%--<option value="student">student</option>
            <option value="teacher">teacher</option>
            <option value="lesson">lesson</option>
            <option value="stu_choose_lesson">stu_choose_lesson</option>
            <option value="leave_apply">leave_apply</option>
            <option value="leave_apply_stat">leave_apply_stat</option>
            <option value="message">message</option>
            <option value="test">test</option>--%>
        </select>
    </form>
</div>
<div class="operateDataDiv" id="hid">
    <form action="myServlet" method="post" id="getForm">
        <table class="dataTable" border="1">
            <tr>
                <% for (int i = 0; i < table.getColumn(); i++) {
                %>
                <td class="tableHeader">
                    <%= table.getElements()[i]%>
                </td>
                <%
                    }
                %>
            </tr>
            <tr>
                <%
                    for (int i = 0; i < table.getColumn(); i++) {
                %>
                <%
                    String info = "info"+i;
                %>
                <td>
                    <input class="table" type="text" name="<%= info %>" />
                </td>
                <%
                    }
                %>
            </tr>
        </table>
        <input class="operateDataBtn" type="submit" value="add" name="op" onclick="setTimeout(successful(),100)"/>
        <input class="operateDataBtn" type="submit" value="update" name="op"  onclick="setTimeout(successful(),100)" />
        <input class="operateDeleteBtn" type="submit" value="delete" name="op" onclick="setTimeout(successful(),100)" />
    </form>
</div>
</body>
</html>

